package com.songlanyun.modules.goodsuserdiscount.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.goods.entity.GoodsSku;
import com.songlanyun.modules.goodsuserdiscount.entity.GoodsUserDiscountConfig;
import com.songlanyun.modules.goodsuserdiscount.model.dto.GoodsUserDiscountDetailDTO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.goodsuserdiscount.dao.GoodsUserDiscountDetailDao;
import com.songlanyun.modules.goodsuserdiscount.entity.GoodsUserDiscountDetail;
import com.songlanyun.modules.goodsuserdiscount.service.GoodsUserDiscountDetailService;


@Service("goodsUserDiscountDetailService")
public class GoodsUserDiscountDetailServiceImpl extends ServiceImpl<GoodsUserDiscountDetailDao, GoodsUserDiscountDetail> implements GoodsUserDiscountDetailService {


    //批量添加商品会员折扣明细
    @Override
    public void batchDetail(Long goodsId, List<GoodsUserDiscountDetail> details) {

        if(details.size()>0)
        {
            clearByGoodsId(goodsId);
            details.forEach(o->{
                o.setGoodsId(goodsId);
            });
            details.forEach(this::create);
        }
    }

    /**
     * 添加商品会员折扣明细
     * @param entity
     */
    private void create(GoodsUserDiscountDetail entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
    }

    public void clearByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsUserDiscountDetail> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(GoodsUserDiscountDetail::getGoodsId,goodsId);
        this.baseMapper.delete(wrapper);
    }
}